import DragModal from '@/components/DragModal';
import { Infos, Pwd } from '@/components/DragModal/formdoms';
import { doFetch } from '@/utils/doFetch';
import { history } from '@umijs/max';
import { Dropdown, theme } from 'antd';
import { useMemo, useState } from 'react';
import useCurrentUser from '@/utils/useCurrentUser';
import 'dayjs/locale/zh-cn';


export default ({ children }) => {
  const [modal, setModal] = useState({
    open: false,
  });

  const {
    token: { colorBgSeven, colorBgThird },
  } = theme.useToken();

  const { getUser, currentUser } = useCurrentUser();

  const items = useMemo(() => {
    return [
      {
        key: '5',
        label: '个人信息',
      },
      {
        key: '3',
        label: '修改密码',
      },
      {
        key: '4',
        danger: true,
        label: '退出登录',
      },
    ]
  }, []);


  return (
    <div>
      <DragModal
        {...modal}
        onCancel={() => {
          setModal((s) => ({
            ...s,
            open: false,
          }));
        }}
        width={516}
        style={{
          '--backgroundColor': colorBgSeven,
          top: '4%',
        }}
      >
        {modal?.title === '修改密码' && (
          <Pwd
            currentUser={currentUser}
            refresh={() => {
              getUser.run();
              setModal((s) => ({
                ...s,
                open: false,
              }));
            }}
          />
        )}
        {modal?.title === '个人信息' && (
          <Infos
            currentUser={currentUser}
            colorBgThird={colorBgThird}
            refresh={() => {
              getUser.run();
            }}
          />
        )}
      </DragModal>
      <Dropdown
        menu={{
          onClick: ({ key }) => {
            switch (key) {
              case '3':
                setModal((s) => ({
                  ...s,
                  open: true,
                  title: '修改密码',
                }));
                break;
              case '4':
                doFetch({ url: '/system/logout', params: {} }).then(() => {
                  localStorage.removeItem('TOKENES');
                  localStorage.setItem('THEME', currentUser?.theme);
                  history.push('/user/login');
                });
                break;
              case '5':
                setModal((s) => ({
                  ...s,
                  open: true,
                  title: '个人信息',
                }));

                break;
              default:
                break;
            }
          },
          items: items,
        }}
      >
        <div>
          {children}
        </div>
      </Dropdown>
    </div>
  );
};
